<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
include('../class.base.php');
if (isset($_GET["tabel"])) {
	$tabel = htmlspecialchars($_GET["tabel"], ENT_QUOTES);
	$id = htmlspecialchars($_GET["id"], ENT_QUOTES);
	$systemTables = getSystemTables();
	if (in_array($tabel, $systemTables)) { die(); }
	if ($tabel == "audio") { $up = "sari"; $digits = 4; $sep = ""; }
	elseif ($tabel == "audio_trakk") { $up = "audio"; $digits = 2; $sep = "-"; }
	elseif ($tabel == "foto") { $up = "fotoalbum"; $digits = 3; $sep = "-"; }
	elseif ($tabel == "fotoalbum") { $up = "sari"; $digits = 4; $sep = ""; }
	elseif ($tabel == "kasikiri") { $up = "sari"; $digits = 4; $sep = ""; }
	elseif ($tabel == "video") { $up = "sari"; $digits = 4; $sep = ""; }
	elseif ($tabel == "video_trakk") { $up = "video"; $digits = 2; $sep = "-"; }
	else { $arhiiviNumber = ""; }
	if (isset($up)) {
		if ($up == "sari") {
			$what = "sarja_tahis";
		}
		else {
			$what = "arhiivinumber";
		}
		$query = mysql_query("SELECT `".$what."` FROM ".$up." WHERE id='".$id."'");
		if (mysql_num_rows($query)) {
			$row = mysql_fetch_array($query);
			$arhiiviNumber = $row[$what];
			$start = mysql_query("SELECT * FROM ".$tabel." WHERE ".$up."='".$id."' ORDER BY arhiivinumber DESC LIMIT 1");
			if (mysql_num_rows($start)) {
				$rida = mysql_fetch_array($start);
				$lastNrParts = explode($arhiiviNumber, $rida["arhiivinumber"]);
				if ($sep == "-") {
					$linesep = explode("-", $lastNrParts[1]);
					$lastNrParts[1] = $linesep[1];
				}
				$newNr = $lastNrParts[1] + 1;
			}
			else {
				$newNr = 1;
			}
			while (strlen($newNr) < $digits) {
				$newNr = "0".$newNr;
			}
			$arhiiviNumber = $arhiiviNumber.$sep.$newNr;
		}
		else {
			$arhiiviNumber = "";
		}
	}
	echo "<input type='text' name='arhiivinumber' onchange=\"ab('".$tabel."');\" value=\"".$arhiiviNumber."\">";
}
?>